package com.newear.marathon.controller;

import com.newear.marathon.annotation.BusinessLogger;
import com.newear.marathon.dto.admin.inquiry.*;
import com.newear.marathon.dto.admin.maintenance.*;
import com.newear.marathon.service.IAdminRoleService;
import com.newear.marathon.utils.ListResponseConverter;
import com.newear.marathon.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MicroBin
 * @since 2019-04-20
 */
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController
{

    @Autowired
    private IAdminRoleService adminRoleService;

    @GetMapping("/inquiry/select")
    @ResponseBody
    public List<Map> roleInquirySelect()
    {
        //调用微服务
        AdminRoleSelectInquiryResponseDTO responseDTO = adminRoleService.doRoleInquirySelect();
        List<AdminRoleSelectInquiryResponseSubDTO> dataList = responseDTO.getDataList();
        //重组响应对象
        List result = new ArrayList();
        dataList.forEach(w -> {
            Map map = new HashMap<>();
            map.put("value", w.getId());
            map.put("name", w.getName());
            map.put("selected", "");
            if (w.getAvailable() == 0)
            {
                map.put("disabled", "disabled");
            }
            else
            {
                map.put("disabled", "");
            }
            result.add(map);
        });
        return result;
    }

    @PostMapping("/inquiry/page")
    @ResponseBody
    public Map roleInquiryPage(Long page, Long limit, String roleName)
    {
        AdminRoleInquiryPageRequestDTO requestDTO = new AdminRoleInquiryPageRequestDTO();
        //组装分页对象
        PageModel pageModel = new PageModel();
        pageModel.setCurrent(page);
        pageModel.setSize(limit);
        requestDTO.setPage(pageModel);
        //查询条件
        requestDTO.setRoleName(roleName);
        //调用微服务
        AdminRoleInquiryPageResponseDTO responseDTO = adminRoleService.doRoleInquiryPage(requestDTO);
        //重组响应对象
        return ListResponseConverter
                .listResponseConverter(responseDTO.getTransactionStatus().isSuccess(), responseDTO.getTransactionStatus().getErrorCode(),
                                       responseDTO.getTransactionStatus().getReplyText(), responseDTO.getPage().getTotal(),
                                       responseDTO.getDataList());
    }

    @PreAuthorize("hasAuthority('role:add')")
    @BusinessLogger(key = "ADMIN", value = "roleAddition")
    @PostMapping("/addition")
    public AdminRoleAdditionResponseDTO roleAddition(@Valid @RequestBody AdminRoleAdditionRequestDTO requestDTO)
    {
        AdminRoleAdditionResponseDTO responseDTO = adminRoleService.doRoleAddition(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "roleModifyInquiry")
    @PostMapping("/modify/inquiry")
    public AdminRoleModifyInquiryResponseDTO roleModifyInquiry(@Valid @RequestBody AdminRoleModifyInquiryRequestDTO requestDTO)
    {
        AdminRoleModifyInquiryResponseDTO responseDTO = adminRoleService.doRoleModifyInquiry(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('role:edit')")
    @BusinessLogger(key = "ADMIN", value = "roleModify")
    @PostMapping("/modify")
    public AdminRoleModifyResponseDTO roleModify(@Valid @RequestBody AdminRoleModifyRequestDTO requestDTO)
    {
        AdminRoleModifyResponseDTO responseDTO = adminRoleService.doRoleModify(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('role:status')")
    @BusinessLogger(key = "ADMIN", value = "roleModifyStatus")
    @PostMapping("/modify/status")
    public AdminRoleModifyStatusResponseDTO roleModifyStatus(@Valid @RequestBody AdminRoleModifyStatusRequestDTO requestDTO)
    {
        AdminRoleModifyStatusResponseDTO responseDTO = adminRoleService.doRoleModifyStatus(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "roleAuthInquiry")
    @PostMapping("/auth/inquiry")
    public AdminRoleAuthInquiryResponseDTO roleAuthInquiry(@Valid @RequestBody AdminRoleAuthInquiryRequestDTO requestDTO)
    {
        AdminRoleAuthInquiryResponseDTO responseDTO = adminRoleService.doRoleAuthInquiry(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('role:auth')")
    @BusinessLogger(key = "ADMIN", value = "roleAuth")
    @PostMapping("/auth")
    public AdminRoleAuthResponseDTO roleAuth(@Valid @RequestBody AdminRoleAuthRequestDTO requestDTO)
    {
        AdminRoleAuthResponseDTO responseDTO = adminRoleService.doRoleAuth(requestDTO);
        return responseDTO;
    }
}
